package com.dsteiner.xelavos.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.PopupOverlayWidget;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.controller.ConfirmPopupController;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ConfirmPopupWidget extends LazyWidget {

	public static class ConfirmPopupModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
				this);

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName,
					listener);
		}
		
		private String message;

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			String oldMessage = this.message;
			this.message = message;
			propertyChangeSupport.firePropertyChange("message", oldMessage, message);
		}
	}

	private ConfirmPopupController confirmPopupController = new ConfirmPopupController();
	private final ConfirmPopupModel confirmPopupModel;

	public ConfirmPopupWidget(ConfirmPopupModel confirmPopupModel) {
		this.confirmPopupModel = confirmPopupModel;
		
		PopupOverlayModel popupOverlayModel = new PopupOverlayModel();
		PopupOverlayWidget popupOverlayWidget = new PopupOverlayWidget(popupOverlayModel);
		popupOverlayModel.setContentWidget(this);
		
		confirmPopupController.registerConfirmPopupWidget(this);
		confirmPopupController.registerPopupOverlayModel(popupOverlayModel);
		confirmPopupController.registerEventManager(EventManagerImpl.getInstance());
		confirmPopupController.registerConfirmPopupModel(confirmPopupModel);
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		HTMLExt messageHtml = new HTMLExt();
		ButtonExt cancelButton = new ButtonExt("Cancelar");
		ButtonExt okButton = new ButtonExt("OK");
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSpacing(5);
		
		mainPanel.add(messageHtml);
		mainPanel.add(horizontalPanel);
		
		horizontalPanel.add(cancelButton);
		horizontalPanel.add(okButton);
		
		confirmPopupController.registerMessageHtml(messageHtml);
		confirmPopupController.registerCancelButton(cancelButton);
		confirmPopupController.registerOkButton(okButton);
		return mainPanel;
	}

}